package org.sample.vm.classloading;

import java.lang.reflect.Method;

public class JavaClassExecuter {
	
	public static String execute(byte[] classBytes) {
		HackSystem.clearBuffer();
		ClassModifier classModifier = new ClassModifier(classBytes);
		String systemClassName = System.class.getName().replaceAll("\\.", "/");
		String hackSystemClassName = System.class.getName().replaceAll("\\.", "/");
		byte[] modifiedBytes = classModifier.modifyUTF8Constant(
				systemClassName, hackSystemClassName);
		HotSwapClassLoader loader = new HotSwapClassLoader();
		Class<?> clazz = loader.loadByte(modifiedBytes);
		try {
			Method method = clazz.getMethod("main", new Class[] { String[].class });
			method.invoke(null, new Object[] { new String[] { null } });
		} catch (Throwable e) {
			e.printStackTrace(HackSystem.out);
		}
		return HackSystem.getBufferString();
	}

}
